<template>
  <div class="login-container">
    <div class="form-container">
      <img src="../assets/logo.png" alt="" />
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>

        <el-form-item class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button class="btncolor" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { loginApi } from "../api/";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],

        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert("submit!");
          // 二次校验
          // 1.获取用户名和密码
          // 2.发送axios请求
          // 2.1 创建utils/index.js  封装api 接口函数
          // 3. 登录成功 就跳转
          // 4.登录失败弹出提示信息
          try {
            const res = await loginApi(this.ruleForm);
            console.log(this.ruleForm);
            console.log(res, 666);
            if (res.data.meta.status == 200) {
              //编程式导航 登录成功
              // console.log(this, 11);
              //存储token 如果存在token 那就是登陆过
              localStorage.setItem("token", res.data.data.token);
              this.$router.push("/welcome");
            } else {
              //登录失败
              this.$message.error("登录失败，请重新登陆！");
            }
          } catch (error) {
            this.$message.error("错了哦，这是一条错误消息");
          }
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang='less'  scoped>
.login-container {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #2b4b6b;
  .form-container {
    width: 450px;
    height: 304px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0px 0px 10px 0px #ccc;
    img {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      background-color: #efefef;
      border: 8px solid #fff;
      width: 120px;
      height: 120px;
      box-shadow: 0px 0px 10px 0px #ccc;
    }
    .btncolor {
      background-color: #ddd;
    }
    .el-form {
      padding: 90px 20px;
      .login-btn {
        text-align: right;
      }
    }
  }
}
</style>